<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends CI_Controller {

	
	private $_data = array();
	public $type=array(
		
	);
	public function __construct(){
		parent::__construct();
		$this->load->helper('url');	

		$this->load->model('product_model');
	}
	public function t(){
		
	}

	/**
	产品首页
	*/
	public function index($cur_page = 0){

		$cond = NULL;
		$total_d = $this->product_model->get_lists('a.id',$cond,'a.id',null);
		
		$this->_data['title'] = '产品';
		
		//分页
		$per_page =8;
		$this->load->library('pagination');
		
		$cf = array(
			'base_url'		=> site_url('product/index'),
			'total_rows'	=> count($total_d),
			'per_page'		=> $per_page
			);
		//$cf['uri_segment'] = 2;
		$this->pagination->initialize($cf);
		$this->_data['pagination'] = $this->pagination->create_links();

		
		$d = $this->product_model->get_lists("a.*, b.title as typetitle", $cond, 'a.id desc ', $per_page, $cur_page);
		$this->_data['data'] = $d;

		
		$this->load->view('product/index', $this->_data);
	}
	//$key_word='', $cur_page = 0
	public function search(){

		$cond = NULL;
		$key_word = $this->input->get('productsearch');
		if($key_word!=''){
			$cond = "a.title like '%{$key_word}%'";
		}



		$total_d = $this->product_model->get_lists('a.id',$cond,'a.id',null);
		
		$this->_data['title'] = '搜索'. $key_word;
		$this->_data['key_word'] = $key_word;
		
		//分页
		$per_page =8;
		$this->load->library('pagination');
		$cur_page = $this->uri->segment(3);
		$cf = array(
			'base_url'		=> site_url('product/search'),
			'first_url'		=> site_url('product/search?productsearch='.$key_word),
			'total_rows'	=> count($total_d),
			'per_page'		=> $per_page,
			'uri_segment'	=> 3,
			// 'page_query_string' => TRUE
			'anchor_class'	=> 'class="pagehw1"',
			'suffix' =>'?productsearch='.$key_word
			);
		//$cf['uri_segment'] = 2;
		$this->pagination->initialize($cf);
		$this->_data['pagination'] = '<div class="page_hw1div">'. $this->pagination->create_links() . '</div>';

		
		
		$d = $this->product_model->get_lists("a.*, b.title as typetitle", $cond, 'a.id desc ', $per_page, $cur_page);
		$this->_data['data'] = $d;

		
		$this->load->view('product/index', $this->_data);
	}

	/**
	* 分类查看
	*/
	public function myclass($typeid, $cur_page = 0){
		if(!$typeid){
			show_404();exit();
		}
		$q = $this->db->get_where('producttype',array('id'=>$typeid));
		$a = $q->result();

		if(count($a) == 0){
			redirect('product');
		}
		$this->_data['title'] =  $a[0]->title;

		$total_d = $this->product_model->get_lists('a.id',array('a.typeid'=>$typeid),'a.id',null);
		
		//分页
		$per_page =8;
		$this->load->library('pagination');
		
		$cf = array(
			'base_url'		=> site_url('product/myclass/'.$typeid),
			'total_rows'	=> count($total_d),
			'per_page'		=> $per_page,
			'uri_segment'	=> 4
			);
		//$cf['uri_segment'] = 2;
		$this->pagination->initialize($cf);
		$this->_data['pagination'] = $this->pagination->create_links();

		
		$d = $this->product_model->get_lists("a.*, b.title as typetitle", array('a.typeid'=>$typeid), 'a.id desc ', $per_page, $cur_page);
		$this->_data['data'] = $d;

		
		$this->load->view('product/myclass', $this->_data);

	}


	public function show( $id=0 ){ 		
		$pro = $this->product_model->get_products('pt.title as typename,p.*','p.id='.$id, 1);
		if(!$pro && !$pro[0]){
//			show_404();
			redirect('product');
		}else{
			$mypro = $pro[0];
			$this->db->where('id',$mypro->id);
			$this->db->update('product', array('views'=>intval($mypro->views)+1));
		}
		$this->_data['product'] = $pro[0];		
		
		$this->_data['title'] = $pro[0]->title.'产品介绍';
		$this->load->view('product/show', $this->_data);
		
	}
	/*public function index(){
		$d = $this->product_model->get_products("*, pt.id as ptid, p.id as id",null,4, 'p.id desc');
		$this->_data['product'] = $d;
			$this->_data['title'] = '产品';
		$this->load->view('header',$this->_data);
		$this->load->view('product', $this->_data);
		
	}

	public function lists($typeid=0, $curpage=0){
		
		$product_type = $this->product_model->get_ptype($typeid);
		$this->_data['product_type'] = $product_type[0];
		$this->_data['title'] = $product_type[0]->title.' 列表';
		$total_data = $this->product_model->get_products('p.id','typeid='.$typeid);
		
		$perpage = 3;
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/product/lists/'.$typeid;
		$config['base_url'] = site_url('/product/lists').'/'.$typeid;
		$config['total_rows'] = count($total_data);
		$config['per_page'] = $perpage; 
		$config['uri_segment'] = 4; 
		
		$this->pagination->initialize($config); 
		
		$this->_data['pagination'] = $this->pagination->create_links();
		
		$limit = $curpage .','.$perpage;
		$this->_data['product'] = $this->product_model->get_products('pt.title as typename, p.*', "typeid=$typeid", $limit);
		$this->_data['producttype'] = $this->product_model->get_producttype();
		$this->load->view('header', $this->_data);
		$this->load->view('product_lists', $this->_data);
	}
	*/
	
}

/* End of file product.php */
/* Location: ./application/controllers/product.php */